package com.graduation.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import cn.circle.commom.controller.BackendBaseController;
import cn.circle.system.util.DataGrid;
import cn.circle.system.util.MD5;
import cn.circle.system.util.PageHelper;

import com.graduation.system.model.SysDepartment;
import com.graduation.system.model.SysUser;

import cn.circle.system.service.ISysDepartmentService;
import cn.circle.system.service.ISysUserService;
import cn.circle.system.util.ReturnInfo;

@Controller
@EnableWebMvc
@RequestMapping("/manage/sysUser")
public class SysUserController extends BackendBaseController{
	
	private static final Log log = LogFactory.getLog(SysUserController.class);
	
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysDepartmentService sysDepaermentService;
	
	/**
	 * 跳转到User首页
	 */
	@RequestMapping(value = "/index")
	public String sysUserIndex(Map<String, Object> map) {
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("dep", dep);
		return "/WEB-INF/jsp/system/system/User/sys_User_list.jsp";
	}
	
	/**
	 * 查询客户信息
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute SysUser c, String userName, String school, PageHelper ph,HttpServletRequest request) {
		
		ph.setSort("custId");
		ph.setOrder("desc");
		DataGrid dg = sysUserService.datagrid(ph,c,school,userName);
		return dg;
	}
	/**
	 * 跳转至 sysUser_au页(添加和修改共用一个编辑页)
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		log.info("----Ok----");
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("saveUrl", "/manage/sysUser/addSave");
		map.put("dep", dep);
		return "/WEB-INF/jsp/system/system/User/sys_User_au.jsp";
	}
	/**
	 * 跳转至 sysUser_au页
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		log.info(id);
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("saveUrl", "/manage/sysUser/editSave");
		map.put("dep", dep);
		if (id != null) {
			List<SysUser> model = sysUserService.getUser(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/jsp/system/system/User/sys_User_au.jsp";
	}
	/**
	 * 改变状态status
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String id, ReturnInfo result) {
		
		sysUserService.remove(id);
		System.out.println("1");
		return result;
	}
	/**
	 * 删除若干条数据
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String id, ReturnInfo result) {
		if(id !=null){
			try {
				sysUserService.delete(id);
			} catch (Exception e) {
				log.info("删除失败"+ e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}
		return result;
	}
	/**
	 * 获得客户详细资料
	 */
	@RequestMapping(value = "/queryUserDetail")
	public String queryCustDetail(String id, Map<String, Object> map) {
		log.info(id);
		if (id != null) {
			List<SysUser> model = sysUserService.getUser(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/jsp/system/system/User/sys_User_detail.jsp";
	}
	/**
	 * 保存添加数据
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid SysUser sysUser, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				System.out.println(error.getField());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysUser != null) {
				//log.info(sysCustomer.getEmail());
				try {
					sysUser.setPassWord(MD5.md5(sysUser.getPassWord()));
					sysUserService.save(sysUser);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}
			}
		}

		return result;
	}
	/**
	 * 保存修改的数据
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid SysUser sysUser, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysUser != null) {
				try {
					sysUserService.update(sysUser);
					//sysCustomerService.saveOrUpdate(sysCustomer);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}
	
	/**
	 * 获取部门信息
	 */
	public DataGrid getDepartment(){
		return sysDepaermentService.datagrid();
	}
}
